﻿Public Class ClassTextOp

    Const Quot As String = Chr(34)

    Public Enum EnumTextOperation
        Nil = 0
        RemplacerTexte = 1
        RemplacerRegEx = 2
        RemplacerTableCar = 3
        Majuscules = 4
        Minuscules = 5
        Justifier = 6
        AjusterLargeur = 7
        MettreEspacesEnColonnes = 8
        MettreVirgulesEnColonnes = 9
        MettreEnColonnes = 10
        Trim = 11
        TrimAvant = 12
        TrimApres = 13
        SupprimerDoubles = 14
        TrierLignes = 15
        TrierEspaces = 16
        TrierVirgules = 17
        Trier = 18
        AlignerPonctuation = 19
        AlignerEgal = 20
        Aligner = 21
        CarUnicode = 22
        UnicodeCar = 23
        LignesListe = 24
        ListeLignes = 25
        Encadrer = 26
        EnleverAccents = 27
        SupprimerLignesVides = 28
        Pivoter = 29
        FusionnerLignes = 30
        ScriptRemplacement = 31
        SeparerLignes = 32
        PremiereLettre = 33
        Lookup = 34
        Numeroter = 35
        Denumeroter = 36
    End Enum

    Public Operation As EnumTextOperation
    Public Parametres(4) As String

    Public Sub New()
        Me.Operation = EnumTextOperation.Nil
        For i = 0 To Parametres.GetUpperBound(0)
            Parametres(i) = ""
        Next
    End Sub

    Public ReadOnly Property Caption() As String
        Get
            Select Case Me.Operation
                Case EnumTextOperation.ScriptRemplacement
                    Return Me.Operation.ToString & "(" & Quot & Me.Parametres(0).Replace(vbCrLf, "|").PadRight(30).Substring(0, 30) & "..." & Quot & "," & Quot & Me.Parametres(1) & Quot & ")"
                Case EnumTextOperation.RemplacerTexte
                    Return Me.Operation.ToString & "(" & Quot & Me.Parametres(0) & Quot & "," & Quot & Me.Parametres(1) & Quot & "," & Quot & Me.Parametres(2) & Quot & ")"
                Case EnumTextOperation.RemplacerRegEx, EnumTextOperation.RemplacerTableCar
                    Return Me.Operation.ToString & "(" & Quot & Me.Parametres(0) & Quot & "," & Quot & Me.Parametres(1) & Quot & ")"
                Case EnumTextOperation.AjusterLargeur
                    Return Me.Operation.ToString & "(" & Me.Parametres(0) & ")"
                Case EnumTextOperation.MettreEnColonnes, EnumTextOperation.Trier, EnumTextOperation.Aligner, EnumTextOperation.SeparerLignes
                    Return Me.Operation.ToString & "(" & Quot & Me.Parametres(0).PadRight(10) & Quot & ")"
                Case EnumTextOperation.Lookup
                    Dim sCaption As String = Me.Operation.ToString & "("
                    For i = 0 To 4
                        sCaption &= Quot & Parametres(i) & Quot
                        If i < 4 Then sCaption &= ","
                    Next
                    Return sCaption
                Case Else
                    Return Me.Operation.ToString()
            End Select
        End Get
    End Property

    Public ReadOnly Property ToMacro() As String
        Get
            Select Case Me.Operation
                Case EnumTextOperation.AjusterLargeur, EnumTextOperation.MettreEnColonnes, EnumTextOperation.Trier, EnumTextOperation.Aligner, EnumTextOperation.SeparerLignes
                    'Un paramètre
                    Return Quot & Me.Operation.ToString & Quot & "," & Me.Parametres(0)
                Case EnumTextOperation.ScriptRemplacement, EnumTextOperation.RemplacerRegEx, EnumTextOperation.RemplacerTableCar
                    'Deux paramètres
                    Return Quot & Me.Operation.ToString & Quot & "," & Quot & Me.Parametres(0) & Quot & "," & Quot & Me.Parametres(1) & Quot
                Case EnumTextOperation.RemplacerTexte
                    'Trois paramètres
                    Return Quot & Me.Operation.ToString & Quot & "," & Quot & Me.Parametres(0) & Quot & "," & Quot & Me.Parametres(1) & Quot & "," & Quot & Me.Parametres(2) & Quot
                Case EnumTextOperation.Lookup
                    '4 paramètres
                    Dim sCaption As String = Quot & Me.Operation.ToString & Quot & ","
                    For i = 0 To 4
                        sCaption &= Quot & Parametres(i) & Quot
                        If i < 4 Then sCaption &= ","
                    Next
                    Return sCaption
                Case Else
                    'Aucun paramètres
                    Return Quot & Me.Operation.ToString & Quot
            End Select
        End Get
    End Property

    Public Function EditParams() As Boolean
        EditParams = True
        Select Case Me.Operation
            Case EnumTextOperation.ScriptRemplacement
                If Me.Parametres(0) <> "" Then FormFiltreScript.TextBox1.Text = Me.Parametres(0)
                If Form1.RichTextBox1.SelectionLength > 0 Then FormFiltreScript.RichComboBoxFiltre.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                If Me.Parametres(1) = "global" Then
                    FormFiltreScript.RadioButton1.Checked = True
                Else
                    FormFiltreScript.RadioButton2.Checked = True
                End If
                If FormFiltreScript.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreScript.TextBox1.Text
                    If FormFiltreScript.RadioButton1.Checked Then
                        Me.Parametres(1) = "global"
                    Else
                        Me.Parametres(1) = "lignes"
                    End If
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.RemplacerTexte
                FormFiltreTexte.TextBox1.Text = Me.Parametres(0)
                FormFiltreTexte.TextBox2.Text = Me.Parametres(1)
                FormFiltreTexte.CheckBox1.Checked = (Me.Parametres(2) = CheckState.Checked.ToString)
                If Form1.RichTextBox1.SelectionLength > 0 Then
                    FormFiltreTexte.RichTextBox1.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                    If Not Form1.RichTextBox1.SelectedText.Contains(vbLf) Then FormFiltreTexte.TextBox1.Text = Form1.RichTextBox1.SelectedText
                End If
                If FormFiltreTexte.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreTexte.TextBox1.Text
                    Me.Parametres(1) = FormFiltreTexte.TextBox2.Text
                    Me.Parametres(2) = FormFiltreTexte.CheckBox1.Checked.ToString
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.RemplacerRegEx
                FormFiltreRegEx.ComboBox1.Text = Me.Parametres(0)
                FormFiltreRegEx.TextBox2.Text = Me.Parametres(1)
                If Form1.RichTextBox1.SelectionLength > 0 Then
                    FormFiltreRegEx.RichTextBox1.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                    If Not Form1.RichTextBox1.SelectedText.Contains(vbLf) Then FormFiltreRegEx.ComboBox1.Text = System.Text.RegularExpressions.Regex.Escape(Form1.RichTextBox1.SelectedText)
                End If
                If FormFiltreRegEx.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreRegEx.ComboBox1.Text
                    Me.Parametres(1) = FormFiltreRegEx.TextBox2.Text
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.RemplacerTableCar
                FormFiltreTableCar.TextBox1.Text = Me.Parametres(0)
                FormFiltreTableCar.TextBox2.Text = Me.Parametres(1)
                If Form1.RichTextBox1.SelectionLength > 0 Then FormFiltreTableCar.RichTextBox1.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                If FormFiltreTableCar.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreTableCar.TextBox1.Text
                    Me.Parametres(1) = FormFiltreTableCar.TextBox2.Text
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.AjusterLargeur
                If Me.Parametres(0) = "" Then Me.Parametres(0) = "80"
                FormFiltreLargeur.NumericUpDown1.Value = CInt(Me.Parametres(0))
                If FormFiltreLargeur.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreLargeur.NumericUpDown1.Value.ToString
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.MettreEnColonnes
                If Me.Parametres(0) = "" Then Me.Parametres(0) = ","
                FormFiltreSepColonnes.TextBox1.Text = Me.Parametres(0)
                If Form1.RichTextBox1.SelectionLength > 0 Then FormFiltreSepColonnes.RichTextBox1.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                If FormFiltreSepColonnes.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreSepColonnes.TextBox1.Text
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.SeparerLignes
                If Me.Parametres(0) = "" Then Me.Parametres(0) = "\s+"
                FormFiltreSepareLignes.TextBox1.Text = Me.Parametres(0)
                If Form1.RichTextBox1.SelectionLength > 0 Then FormFiltreSepareLignes.RichTextBox1.Text = Form1.RichTextBox1.SelectedText
                If FormFiltreSepareLignes.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreSepareLignes.TextBox1.Text
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.Trier
                If Me.Parametres(0) = "" Then Me.Parametres(0) = ","
                FormFiltreTri.TextBox1.Text = Me.Parametres(0)
                If Form1.RichTextBox1.SelectionLength > 0 Then FormFiltreTri.RichTextBox1.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                If FormFiltreTri.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreTri.TextBox1.Text
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.Aligner
                If Me.Parametres(0) = "" Then Me.Parametres(0) = "-"
                FormFiltreAligner.TextBox1.Text = Me.Parametres(0)
                If Form1.RichTextBox1.SelectionLength > 0 Then FormFiltreAligner.RichTextBox1.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                If FormFiltreAligner.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreAligner.TextBox1.Text
                Else
                    EditParams = False
                End If
            Case EnumTextOperation.Lookup
                'Liste des documents courants
                FormFiltreLookup.ComboBoxDocuments.Items.Clear()
                For Each oItem In Form1.ListViewFichiers.Items
                    FormFiltreLookup.ComboBoxDocuments.Items.Add(oItem.Tag.Chemin)
                Next
                If String.Concat(Me.Parametres) <> "" Then
                    'On laisse le contenu intact si on a pas de paramètres
                    FormFiltreLookup.ComboBoxFindRegex.Text = Me.Parametres(0)
                    FormFiltreLookup.TextBoxReplace.Text = Me.Parametres(1)
                    FormFiltreLookup.TextBoxLookupRegex.Text = Me.Parametres(2)
                    FormFiltreLookup.ComboBoxDocuments.Text = Me.Parametres(3)
                    FormFiltreLookup.TextBox1.Text = Me.Parametres(4)
                End If
                If Form1.RichTextBox1.SelectionLength > 0 Then
                    FormFiltreLookup.RichTextBoxEssai.Text = Form1.RichTextBox1.SelectedText.Replace(vbLf, vbCrLf)
                ElseIf FormFiltreLookup.RichTextBoxEssai.Text = "" Then
                    FormFiltreLookup.RichTextBoxEssai.Text = Form1.RichTextBox1.Text
                End If
                If FormFiltreLookup.ShowDialog() = DialogResult.OK Then
                    Me.Parametres(0) = FormFiltreLookup.ComboBoxFindRegex.Text
                    Me.Parametres(1) = FormFiltreLookup.TextBoxReplace.Text
                    Me.Parametres(2) = FormFiltreLookup.TextBoxLookupRegex.Text
                    Me.Parametres(3) = FormFiltreLookup.ComboBoxDocuments.Text
                    Me.Parametres(4) = FormFiltreLookup.TextBox1.Text
                Else
                    EditParams = False
                End If
        End Select

    End Function

    Public Function Transformer(ByVal Chaine As String) As String
        Select Case Me.Operation
            Case EnumTextOperation.SeparerLignes
                If Me.Parametres(0) <> "" Then
                    Return Commun.SeparerLignes(Chaine, Me.Parametres(0))
                Else
                    Return Chaine
                End If
            Case EnumTextOperation.RemplacerTexte
                If Me.Parametres(0) <> "" Then
                    Dim bMultiple As Boolean = (Me.Parametres(2) = Boolean.TrueString)
                    Return Commun.RemplacerTexte(Chaine, Me.Parametres(0), Me.Parametres(1), bMultiple)
                Else
                    Return Chaine
                End If
            Case EnumTextOperation.RemplacerRegEx
                If Me.Parametres(0) <> "" Then
                    Try
                        Return System.Text.RegularExpressions.Regex.Replace(Chaine, Commun.ExpandRegex(Parametres(0)), Parametres(1), System.Text.RegularExpressions.RegexOptions.IgnoreCase Or System.Text.RegularExpressions.RegexOptions.Multiline)
                    Catch Ex As Exception
                        MsgBox("Erreur d'expression régulière:" & Ex.Message, MsgBoxStyle.Exclamation)
                        Return Chaine
                    End Try
                Else
                    Return Chaine
                End If
            Case EnumTextOperation.RemplacerTableCar
                If Me.Parametres(0) <> "" Then
                    Return Commun.RemplaceCar(Chaine, Me.Parametres(0), Me.Parametres(1))
                Else
                    Return Chaine
                End If
            Case EnumTextOperation.Majuscules
                Return Chaine.ToUpper()
            Case EnumTextOperation.Minuscules
                Return Chaine.ToLower()
            Case EnumTextOperation.Justifier
                Return Commun.Justifier(Chaine)
            Case EnumTextOperation.AjusterLargeur
                Return Commun.Reformater(Chaine, CInt(Me.Parametres(0)))
            Case EnumTextOperation.MettreEspacesEnColonnes
                Return Commun.MettreEnColonnes(Chaine, "\s+")
            Case EnumTextOperation.MettreVirgulesEnColonnes
                Return Commun.MettreEnColonnes(Chaine, ",")
            Case EnumTextOperation.MettreEnColonnes
                Return Commun.MettreEnColonnes(Chaine, Me.Parametres(0))
            Case EnumTextOperation.Trim
                Return Commun.TrimTout(Chaine)
            Case EnumTextOperation.TrimAvant
                Return Commun.TrimAvant(Chaine)
            Case EnumTextOperation.TrimApres
                Return Commun.TrimApres(Chaine)
            Case EnumTextOperation.SupprimerDoubles
                Return Commun.SupprimerLignesDoubles(Chaine)
            Case EnumTextOperation.TrierLignes
                Return Commun.Trier(Chaine, vbLf)
            Case EnumTextOperation.TrierEspaces
                Return Commun.Trier(Chaine, "\s+")
            Case EnumTextOperation.TrierVirgules
                Return Commun.Trier(Chaine, ",")
            Case EnumTextOperation.Trier
                Return Commun.Trier(Chaine, Me.Parametres(0))
            Case EnumTextOperation.AlignerPonctuation
                Return Commun.Aligner(Chaine, "[\.,:;]")
            Case EnumTextOperation.AlignerEgal
                Return Commun.Aligner(Chaine, "=")
            Case EnumTextOperation.Aligner
                Return Commun.Aligner(Chaine, Me.Parametres(0))
            Case EnumTextOperation.CarUnicode
                Return Commun.cStringToUnicode(Chaine)
            Case EnumTextOperation.UnicodeCar
                Return Commun.cUnicodeToString(Chaine)
            Case EnumTextOperation.LignesListe
                Return Commun.LigneListe(Chaine)
            Case EnumTextOperation.ListeLignes
                Return Commun.ListeLigne(Chaine)
            Case EnumTextOperation.Encadrer
                Return Commun.Encadrer(Chaine, "-")
            Case EnumTextOperation.EnleverAccents
                Return Commun.RemplaceCar(Chaine, "àÀâÂäÄçÇéÉèÈêÊëËîÎïÏôÔöÖûÛüÜ", "aAaAaAcCeEeEeEeEiIiIoOoOuUuU")
            Case EnumTextOperation.SupprimerLignesVides
                Return Commun.SupprimerLignesVides(Chaine)
            Case EnumTextOperation.Pivoter
                Return Commun.Pivoter(Chaine)
            Case EnumTextOperation.FusionnerLignes
                Return Commun.FusionnerLignes(Chaine)
            Case EnumTextOperation.Numeroter
                Return Commun.Numeroter(Chaine)
            Case EnumTextOperation.Denumeroter
                Return Commun.DeNumeroter(Chaine)
            Case EnumTextOperation.ScriptRemplacement
                Dim oScript As New ClassMacro
                oScript.Titre = "Script de remplacement"
                Chaine = Chaine.Replace(vbCrLf, vbLf)
                If Me.Parametres(1) = "global" Then
                    If Chaine.Contains(vbLf) Then
                        oScript.Code = "Texte = " & Quot & Quot & vbCrLf
                        For Each sLigne As String In Split(Chaine, vbLf)
                            oScript.Code &= "Texte = Texte & " & Quot & sLigne.Replace(Quot, Quot & Quot) & Quot & " & vbcrlf" & vbCrLf
                        Next
                    Else
                        oScript.Code = "Texte = " & Quot & Chaine.Replace(Quot, Quot & Quot) & Quot & vbCrLf
                    End If
                    oScript.Code &= Me.Parametres(0)
                    Return oScript.EvalVBScript("Texte")
                Else
                    Dim Result As String = ""
                    For Each sLigne As String In Split(Chaine, vbLf)
                        oScript.Code = "Texte = " & Quot & sLigne.Replace(Quot, Quot & Quot) & Quot & vbCrLf
                        oScript.Code &= Me.Parametres(0)
                        Result &= oScript.EvalVBScript("Texte") '&vbcrlf
                        If Chaine.Contains(vbLf) Then Result &= vbCrLf
                    Next
                    Return Result
                End If
            Case EnumTextOperation.PremiereLettre
                Return StrConv(Chaine, VbStrConv.ProperCase)
            Case EnumTextOperation.Lookup
                Return Commun.Lookup(Chaine, Me.Parametres(0), Me.Parametres(1), Me.Parametres(2), Me.Parametres(3), Me.Parametres(4))
            Case Else
                Return Chaine
        End Select
    End Function
End Class
